package com.fastadmin.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.fastadmin.mapper.impl.${className}Mapper;
import com.fastadmin.pojo.${className};
import com.fastadmin.pojo.FastParam;
import com.xphsc.easyjdbc.page.PageInfo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequiredArgsConstructor
@Slf4j
public class ${className}Controller extends BaseController {

    private final ${className}Mapper ${classname}Mapper;

    @GetMapping("sys/${classname}/index")
    public String index() {
        return "sys/${classname}/index";
    }

    @GetMapping("sys/${classname}/add")
    public String add() {
        return "sys/${classname}/add";
    }

    @GetMapping("sys/${classname}/edit")
    public String index(@RequestParam Long ids, Model model) {
        ${className} ${classname} = this.${classname}Mapper.findById(ids);
        model.addAttribute("${classname}", ${classname});
        return "sys/${classname}/edit";
    }

    @RequestMapping("sys/${classname}/data")
    @ResponseBody
    public Object data(FastParam fastParam) {
        PageInfo<${className}> pageInfo = this.${classname}Mapper.findByFastParam(fastParam);
        return datagrid(pageInfo);
    }

    @RequestMapping("sys/${classname}/del")
    @ResponseBody
    public Object del(String ids) {
        String[] split = StrUtil.split(ids, ",");
        for (String s : split) {
            ${classname}Mapper.deleteById(s);
        }
        return ok();
    }

    @RequestMapping("sys/${classname}/save")
    @ResponseBody
    public Object save(${className} ${classname}) {
        if (ObjectUtil.isNotNull(${classname}.getId())) {
            //修改
            ${classname}Mapper.update(${classname});
        } else {
            //保存
            ${classname}Mapper.save(${classname});
        }
        return ok();
    }

}
